<?php
namespace Report\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $admin = session('report');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $model=M('menu');
        $menu=$model->field('menu.menu_id,menu.menu_name,menu.menu_href,menu.menu_parent,menu.`class`')
            ->join('interaction ON menu.menu_id = interaction.inter_mid','left')
            ->where('(interaction.inter_pid='.$admin['user_role'].') AND (menu.switch=1)')->select();//print_r($menu);die;
        $menu_parent=[];
        $menu_child=[];
        foreach ($menu as $k => $v){
            if(empty($v['menu_parent'])){
                $menu_parent[]=$v;
            }else{
                $menu_child[]=$v;
            }
        }
        foreach ($menu_parent as $k => $v){
            $menu_parent[$k]['child']=[];
            foreach ($menu_child as $ke => $va){
                if($v['menu_id']==$va['menu_parent']){
                    $menu_parent[$k]['child'][]=$va;
                }
            }
        }
        //print_r($menu_parent);die;
        $logo=M('logo')->field('id,logo,ico,crm_logo,web_name,web_name_en')->find();
        $this->assign('menu',$menu_parent);
        $this->assign('logo',$logo);
        $this->assign('admin',$admin);
        $this->display();
    }

    public function common_css(){
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        $this->display();
    }

    public function login(){//登录页面
        $this->display();
    }

    public function index_frame(){//浮动窗口主页
        $model=M('user');
        $y = date("Y");
        $m = date("m");
        $d = date("d");
        $from = mktime(0, 0, 0, $m, $d, $y)+1;
        $to = mktime(0, 0, 0, $m, $d, $y)+(24 * 60 * 60);
        //echo date('Y-m-d H:i:s',$from).'******'.date('Y-m-d H:i:s',$to);die;
        $count['user']=$model->where(['user_role'=>1])->count();//MT4用户数
        $count['agent']=$model->where('(user_role=1) and (position>1)')->count();//经纪人用户数
        $count['gold']=M('into_gold')->where('(status=2) and (into_time between '.$from.' and '.$to.')')->sum('final_money');//今日入金
        $count['rebate']=M('rebate_log')->where('time between '.$from.' and '.$to)->sum('money');//今日佣金发放

        $position=M('position')->field('id,user_position')->where('id>1')->select();//职位数据
        $user_group=$model->field('position,count(1)')->where('(user_role=1) and (position>1)')->group('position')->select();
        $userGroup=[];
        foreach ($user_group as $k => $v){
            $userGroup[$v['position']]=$v['count(1)'];
        }
        foreach ($position as $k => $v){
            $position[$k]['count_member']=$userGroup[$v['id']] ? $userGroup[$v['id']] : 0;
        }
        $thing['examine']=$model->where(['status'=>2])->count();//待审核用户数
        $thing['gold']=M('gold')->where(['status'=>1])->count();//提款待处理
        $thing['position_apply']=M('position_apply')->where(['state'=>1])->count();//职位申请待处理
        $thing['work_order']=M('work_order')->where(['state'=>1])->count();//工单待处理
        $thing['lever_apply']=M('lever_apply')->where(['state'=>1])->count();//杠杆申请待处理

        $news=M('article')->field('id,title,add_time,type')->order('id desc')->limit(10)->select();
        $this->assign('count',$count);
        $this->assign('position',$position);
        $this->assign('thing',$thing);
        $this->assign('news',$news);
        $this->display();
    }

    public function loginOut(){
        session('report',null);
        $this->redirect('Home/Index/loginView','正在退出......');
    }
}